//package com.upf1sh.utils.job;
//
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.upf1sh.bean.SensorData;
//import com.upf1sh.mapper.SensorMapper;
//import com.upf1sh.service.SensorDataService;
//import org.apache.http.HttpEntity;
//import org.apache.http.HttpResponse;
//import org.apache.http.client.HttpClient;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.entity.StringEntity;
//import org.apache.http.impl.client.HttpClients;
//import org.apache.http.util.EntityUtils;
//import org.quartz.*;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
//import org.springframework.scheduling.quartz.JobDetailFactoryBean;
//import org.springframework.scheduling.quartz.SchedulerFactoryBean;
//
//import javax.annotation.Resource;
//import java.util.ArrayList;
//import java.util.List;
//
//public class SensorDataJob implements Job {
//
//    private static final Logger log = LoggerFactory.getLogger(SensorDataJob.class);
//
//    @Resource
//    private SensorMapper service;
//
//
//
//
//    @Override
//    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//        List<SensorData> sensorList = new ArrayList<>();
//        //1.请求接口
//        HttpClient client = HttpClients.createDefault();
//        String url = "http://106.12.172.133:9060/api/SensingData/Post_ReadSensingData";
//
//        // 创建HttpPost实例
//        HttpPost post = new HttpPost(url);
//
//        // 创建JSON格式的请求体
//        String json = "{\"user_name\":\"sa\", \"pass_word\":\"ias1957\"}";
//        HttpEntity responseEntity = null;
//        String result = null;
//
//        try {
//            // 设置请求头Content-Type为application/json
//            post.setHeader("Content-Type", "application/json");
//            // 设置请求体
//            StringEntity entity = new StringEntity(json);
//            post.setEntity(entity);
//
//            // 发送POST请求
//            HttpResponse response = client.execute(post);
//
//            // 获取响应实体
//            responseEntity = response.getEntity();
//
//            // 打印响应状态码
////            System.out.println("Response Code: " + response.getStatusLine().getStatusCode());
//
//            // 打印响应内容
//            result = EntityUtils.toString(responseEntity);
//            System.out.println(result);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        //2.封装集合
//        if (responseEntity != null) {
//
//            JSONObject jsonObject = JSONObject.parseObject(result);
//            JSONArray jsonArray = jsonObject.getJSONArray("Values");
//            // 遍历JSON数组并添加到ArrayList中
//            for (int i = 0; i < jsonArray.size(); i++) {
//                JSONObject sensorJsonObject = jsonArray.getJSONObject(i);
//                SensorData sensorData = new SensorData();
//
//                sensorData.setValuesFood(sensorJsonObject.getFloat("ValuesFood"));
//                sensorData.setAwareID(sensorJsonObject.getString("AwareID"));
//                sensorData.setValuesStep(sensorJsonObject.getInteger("ValuesStep"));
//                sensorData.setTimestamp(sensorJsonObject.getTimestamp("Timestamp").toLocalDateTime());
//                sensorData.setValuesTemperature(sensorJsonObject.getFloat("ValuesTemperature"));
//                sensorData.setValuesWater(sensorJsonObject.getFloat("ValuesWater"));
//                sensorList.add(sensorData);
//            }
//            for (SensorData sensorData : sensorList) {
//                System.out.println(sensorData.getValuesFood());
//                service.insert(sensorData);
//            }
//        } else {
//            log.error("猪只传感器数据上传失败！");
//        }
////        System.out.println(sensorList.get(0).getValuesFood());
//        //3.插入数据库
//
//    }
//
//    @Configuration
//    public static class QuartzConfig {
//
//        @Bean
//        public JobDetailFactoryBean sensorDataJobDetail() {
//            JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
//            factoryBean.setJobClass(SensorDataJob.class);
//            factoryBean.setDurability(true);
//            return factoryBean;
//        }
//
//        @Bean
//        public CronTriggerFactoryBean sensorDataJobTrigger(JobDetail sensorDataJobDetail) {
//            CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
//            factoryBean.setJobDetail(sensorDataJobDetail);
//            // 每天 00:00 执行
//            factoryBean.setCronExpression("0 24 23 * * ?");
//            return factoryBean;
//        }
//
//        @Bean
//        public SchedulerFactoryBean schedulerFactory(Trigger sensorDataJobTrigger, JobDetail sensorDataJobDetail) {
//            SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
//            factoryBean.setTriggers(sensorDataJobTrigger);
//            factoryBean.setJobDetails(sensorDataJobDetail);
//            return factoryBean;
//        }
//    }
//}